﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;

namespace IHISCETeim.DAL
{
    public class UTallySurveyDAL
    {
        /// <summary>
        /// 返回-1表示此机器在这时间下已调查过
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(UTallySurveyInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = DB.GetStoredProcCommand("UTallySurvey_Add", obj.MachineType.ToUpper().Trim(), obj.SerialNumber.ToUpper().Trim(), obj.WorkAddress, obj.WorkType, obj.WorkTime, obj.SurveyDate, obj.SurveyHours, obj.IsKeep, obj.AgentName, obj.AgentTel, obj.AgentSvrPer, obj.AgentSvrPerTel);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }

        public static bool Delete(int UTSID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UTallySurvey_Delete", UTSID);
            int result = Convert.ToInt32(DB.ExecuteScalar(dbCommand));
            return result > 0;
        }

        public static IDataReader GetBySerialNumber(string SerialNumber)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UTallySurvey_GetBySerialNumber", SerialNumber.ToUpper().Trim());
            return DB.ExecuteReader(dbCommand);
	    }

        public static DataTable GetPagination(string MachineType, string SerialNumber, string AgentName, string SurveyDateFrom, string SurveyDateTo,int IsKeep, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("UTallySurvey_GetPagination");
            DB.AddInParameter(dbCommand, "MachineType", DbType.StringFixedLength, MachineType.ToUpper().Trim());
            DB.AddInParameter(dbCommand, "SerialNumber", DbType.String, SerialNumber.ToUpper().Trim());
            DB.AddInParameter(dbCommand, "AgentName", DbType.String,AgentName);
            DB.AddInParameter(dbCommand, "SurveyDateFrom", DbType.String,SurveyDateFrom);
            DB.AddInParameter(dbCommand, "SurveyDateTo", DbType.String,SurveyDateTo);
            DB.AddInParameter(dbCommand, "IsKeep", DbType.Int32, IsKeep);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
		    DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32( DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }

        public static DataTable GetReport(string AgentName, string fromdate, string todate)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("UTallySurvey_GetReport", AgentName, fromdate, todate);
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
    }
}
